package headfirst.headfirst.test.subject.impl;

import headfirst.headfirst.test.subject.ObserverInterface;
import headfirst.headfirst.test.subject.SubjectInterface;

import java.util.Date;

/**
 * @author wangding
 * @version V1.0
 * @date 2018/4/9 17:09
 */
public class Observer implements ObserverInterface {

    private SubjectInterface subject;

    //温度
    private String temperature;
    //湿度
    private String humidity;
    //气压
    private String pressure;

    public Observer(SubjectInterface subject) {
        this.subject = subject;
        subject.registerObserver(this);
    }

    public void register(){
        System.out.println("------观察者1订阅成功-------");
        subject.registerObserver(this);
    }

    public void cancelRegister(){
        System.out.println("------观察者1取消订阅了-------");
        subject.removeObserver(this);
    }

    public void update(String temperature, String humidity, String pressure) {
        this.humidity=humidity;
        this.pressure=pressure;
        this.temperature=temperature;
        display();
    }

    private void display() {
        System.out.println("观察者1----数据更新了（"+new Date()+"）-温度："+temperature+"-湿度："+humidity+"-气压："+pressure);
    }

}
